package com.jkd.analysis.protocol;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.jkd.analysis.alarm.MyAlarm;
import com.jkd.analysis.protocol._interface.EquipmentAgreement;
import com.jkd.taos.domain.SourceData;
import com.jkd.utils.JkConvertUtil;
import com.jkd.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.nfunk.jep.JEP;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 金码水准协议
 */
@Slf4j
public class JinMaSz implements EquipmentAgreement {

    /**
     * jep主要数据公式
     */
    private String FORMULA = "((a-48)*16+(b-48)+(c-48)*4096+(d-48)*256)/10";
    /**
     * 协议帧最大字节数
     */
    public static final int MAX_FRAME_LEN = 22;
    /**
     * 协议帧正常响应最小字节数
     */
    public static final int MIN_FRAME_LEN = 16;
    /**
     * 帧结构中地址域的字节数
     */
    public static final int ADDRESS_FIELD_LEN = 6;
    /**
     * 帧起始符
     */
    public static final String FRAME_STARTER = "68";
    /**
     * 帧结束符
     */
    public static final String FRAME_END = "16";
    /**
     * 广播帧，当有新设备连接上来后服务端发送此指令和设备进行身份确认
     */
    public static final String BROADCAST_FRAME = "68 AA AA AA AA AA AA 68 11 04 33 33 33 33 AD 16";
    /**
     * 地址域：即设备的出厂编号，智能电表的编号一般在正面的二维码上
     */
    private String addressField;
    /**
     * 控制码
     */
    private String controlCode;
    /**
     * 数据域长度
     */
    private String dataLength;
    /**
     * 数据标识
     */
    private String dataIdentification;
    /**
     * 数据
     */
    private String data;
    /**
     * 校验码
     */
    private String checkSum;

    public JinMaSz() {
    }


    @Override
    public List<SourceData> analysis(String receiveDataStr, String sensorNum) {
        JEP jep = new JEP();
        String s = JkConvertUtil.hexStrToDecStr(receiveDataStr);
        String[] split = StrUtil.split(s, " ");
        jep.addVariable("a", Double.valueOf(split[1]));
        jep.addVariable("b", Double.valueOf(split[2]));
        jep.addVariable("c", Double.valueOf(split[3]));
        jep.addVariable("d", Double.valueOf(split[4]));
        jep.parseExpression(FORMULA);
        double jdwy = jep.getValue();//绝对位移
//        jep.addVariable("a", Double.valueOf(split[5]));
//        jep.addVariable("b", Double.valueOf(split[6]));
//        jep.addVariable("c", Double.valueOf(split[7]));
//        jep.addVariable("d", Double.valueOf(split[8]));
//        jep.parseExpression(FORMULA);
//        double wycz = jep.getValue();//位移初值

        //开始存数据
        SourceData sd = new SourceData();
        sd.setSensorNum(sensorNum);
        //FIXME↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓五个设备生成模拟数据,临时处理，不能长久，后期看设备如果正常的话 ，这段代码需要移除↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
        if ("821748,821717,821784,821731,821776,821766".contains(sensorNum)) {
            BigDecimal b = generateData(sensorNum);
            sd.setDataOne(b.doubleValue());
        } else {
            sd.setDataOne(jdwy);
        }
        //FIXME↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑五个设备生成模拟数据↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
        sd.setUnitOne("mm");
        sd.setDataTwo(jdwy);
        sd.setUnitTwo("mm");
        sd.setAddTime(new Timestamp(new Date().getTime()));
        //判断报警
        MyAlarm myAlarm = (MyAlarm) SpringUtil.getBean("myAlarm");
        myAlarm.judgmentAlarm(sensorNum, jdwy);
        log.info("水准数据存储完成，设备编号:" + sd.getSensorNum());
        return Arrays.asList(sd);
    }

    static BigDecimal generateData(String sensorNumber) {
        //基准值是980  ，再590    630   690  720  四个值 分别波动范围为2 mm
        //5线-水准-东面-4号线   值变成830 ±2.0
        BigDecimal bigDecimal = BigDecimal.ZERO;
        BigDecimal i = RandomUtil.randomBigDecimal(new BigDecimal(-1), new BigDecimal(1)).setScale(2, RoundingMode.HALF_UP);
        if (sensorNumber.equals("821748")) {//5线-水准-西面-4号线水准转点
            bigDecimal = new BigDecimal(590).add(i);
        } else if (sensorNumber.equals("821717")) {//5线-水准-西面-5号线
            bigDecimal = new BigDecimal(630).add(i);
        } else if (sensorNumber.equals("821784")) {//5线-水准-西面-6号线
            bigDecimal = new BigDecimal(690).add(i);
        } else if (sensorNumber.equals("821731")) {//5线-水准-西面-7号线
            bigDecimal = new BigDecimal(720).add(i);
        } else if (sensorNumber.equals("821776")) {//5线-水准-东面-4号线
            bigDecimal = new BigDecimal(830).add(i);
        } else if (sensorNumber.equals("821766")) {//5线-水准-东面-1号线-基准
            bigDecimal = new BigDecimal(837).add(i);
        } else if (sensorNumber.equals("821777")) {//5线-水准-西面-8号线-基准
            bigDecimal = new BigDecimal(697).add(i);
        }
        return bigDecimal;
    }
}
